Skip to content

Fix #16944: Invalid filtering of IPv6 with FILTER_FLAG_NO_RES_RANGE - #17111

Closed
derickr wants to merge 10 commits into
php:PHP-8.3from
derickr:fix-ip-ranges
Closed

Fix #16944: Invalid filtering of IPv6 with FILTER_FLAG_NO_RES_RANGE#17111
derickr wants to merge 10 commits into
php:PHP-8.3from
derickr:fix-ip-ranges

Conversation

@derickr

@derickr derickr commented Dec 10, 2024

Copy link
Copy Markdown
Member

It also refactors the code to use the actual tables from RFC 6890, and fixed other bugs with these ranges that were introduced with #7893

@derickr

derickr commented Dec 10, 2024

Copy link
Copy Markdown
Member Author

/cc @nielsdos @nicolas-grekas — It makes most sense to review this commit-by-commit.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much much nicer, thanks a lot!

Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errata for RFC6890 changes this, see https://www.rfc-editor.org/errata_search.php?rfc=6890&rec_status=1&presentation=records
Should instead be: /* RFC 0791 - This network */.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually splits up the record into two, so I've made this change instead:

 }
 /* }}} */
 
-/* From the tables in RFC 6890 - Special-Purpose IP Address Registries */
-static bool ipv4_get_status_flags(int ip[8], bool *global, bool *reserved, bool *private)
+/* From the tables in RFC 6890 - Special-Purpose IP Address Registriesi
+ * Including errata: https://www.rfc-editor.org/errata_search.php?rfc=6890&rec_status=1 */
+static bool ipv4_get_status_flags(const int ip[8], bool *global, bool *reserved, bool *private)
 {
        *global = false;
        *reserved = false;
        *private = false;
 
        if (ip[0] == 0) {
+               /* RFC 0791 - This network */
+               *reserved = true;
+       } else if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0) {
                /* RFC 1122 - This host on this network */
                *reserved = true;
        } else if (ip[0] == 10) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. The compiler will optimize that anyway so it's okay to be verbose 🙂

Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range is 192.0.0.0/29. So this seems wrong because it is 192.0.0.0 until 192.0.0.7, because only the 3 bottom bits are free to use. So both the range and indices of the ip in this check are not correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It looks like the original code didn't even check for this one.

Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be const int ip[8] in theory, but doesn't matter much.

@ndossche

Copy link
Copy Markdown
Member

Also note that this should target branch PHP-8.3, see https://externals.io/message/125995

@derickr derickr left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also going to retarget this for PHP 8.3

Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It looks like the original code didn't even check for this one.

Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually splits up the record into two, so I've made this change instead:

 }
 /* }}} */
 
-/* From the tables in RFC 6890 - Special-Purpose IP Address Registries */
-static bool ipv4_get_status_flags(int ip[8], bool *global, bool *reserved, bool *private)
+/* From the tables in RFC 6890 - Special-Purpose IP Address Registriesi
+ * Including errata: https://www.rfc-editor.org/errata_search.php?rfc=6890&rec_status=1 */
+static bool ipv4_get_status_flags(const int ip[8], bool *global, bool *reserved, bool *private)
 {
        *global = false;
        *reserved = false;
        *private = false;
 
        if (ip[0] == 0) {
+               /* RFC 0791 - This network */
+               *reserved = true;
+       } else if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0) {
                /* RFC 1122 - This host on this network */
                *reserved = true;
        } else if (ip[0] == 10) {

@derickr
derickr changed the base branch from PHP-8.2 to PHP-8.3 December 13, 2024 17:10
Comment thread ext/filter/logical_filters.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did another pass over the PR, and I only have one question for this line here.

RFC8190 updates some parts of RFC6890, but the only change relevant to this RP seems to be "2.2. Updates to the IPv4 Special-Purpose Address Registry". It states:

Limited Broadcast prefix (255.255.255.255/32) - The Reserved-by-
Protocol value has changed from False to True. This change was
made to align the registry with reservation of the limited
broadcast address with Section 7 of [RFC919].

So this likely now needs to set reserved to true.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right, thanks

@derickr

derickr commented Dec 18, 2024

Copy link
Copy Markdown
Member Author

I have merged this after a rebase, manually, as GitHub got confused about having changed branches for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants